package com.zsck.zsgy.ui.main.fragment.home

import androidx.lifecycle.MutableLiveData
import com.zsck.zsgy.api.ApiService
import com.zsck.zsgy.base.BaseRepository
import com.zsck.zsgy.http.RetrofitManager
import com.zsck.zsgy.http.exception.ApiException
import com.zsck.zsgy.ui.main.bean.CityModel
import com.zsck.zsgy.ui.main.bean.HomeBean
import kotlinx.coroutines.CoroutineScope

/**
 * 首页数据仓库
 */
class HomeRepo(coroutineScope: CoroutineScope, errorLiveData: MutableLiveData<ApiException>) :
    BaseRepository(coroutineScope, errorLiveData) {

    fun getCityByCityName(cityName: String, cityModel: MutableLiveData<List<CityModel>>) {
        launch(block = {
            RetrofitManager.getApiService(ApiService::class.java)
                .getCityByCityName(cityName)
                .data
        }, success = {
            cityModel.postValue(it)
        })
    }

    fun getHomeData(
        cityId: String,
        cityName: String,
        latitude: String,
        longitude: String,
        homeModel: MutableLiveData<HomeBean>
    ) {
        var body = HashMap<String, String>()
        body.put("cityId",cityId)
        body.put("cityName",cityName)
        body.put("latitude",latitude)
        body.put("longitude",longitude)
        launch(block = {

            RetrofitManager.getApiService(ApiService::class.java)
                .postGetHome(body)
                .data
        }, success = {
            homeModel.postValue(it)
        })
    }

}